<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Manipulates cookies.
 */
class Cookies
{
	/**
	 * Does a cookie exists ?
	 * 
	 * @param  $key string Cookie name.
	 * @return True if cookie exists. 
	 */
	function check($key)
	{
		return isset($_COOKIE[$key]);
	}
	
	/**
	 * Reads a cookie.
	 *
	 * The value may be any PHP variable type (integer, string, array, etc).
	 */
	function read($key)
	{
		return Cookies::check($key) ? unserialize(base64_decode($_COOKIE[$key])) : null;
	}
	
	/**
	 * Creates a cookie.
	 * 
	 * The value may be any PHP variable type (integer, string, array, etc).
	 */
	function write($key, $value, $expire='+1 month', $path='/', $domain=null, $secure=false)
	{
		if (is_string($expire))
			$expire = strtotime($expire);
		
		if (is_null($domain))
			$domain = Core::session_domain;
		
		$value = base64_encode(serialize($value));
		
		return setcookie($key, $value, $expire, $path, $domain, $secure);
	}
	
	/**
	 * Deletes a cookie.
	 */
	function delete($key)
	{
		return Cookies::write($key, '', '-1 month');
	}
}
?>